var express = require('express');
var router = express.Router();


router.get('/', function (req, res, next) {
  res.render('login');
});
router.get('/logout', function(req, res, next) {
  req.session.username = null;
  res.redirect('/');
})

router.post('/', function (req, res, next) {
  var obj = req.body;
  var cfg = require('../config');
  var whiteIp = cfg.whiteIp;
  if (-1 == whiteIp.indexOf(req.ip)) {
    console.log(req.ip, "not in the whiteIp");
    return res.status(404).send("LOGIN ERR");
  }
  var acc = cfg.acc;
  if (obj.username == acc.username && obj.password == acc.password) {
    req.session.username = acc.username;
    res.send("LOGIN SUCCESS");
  } else {
    res.status(404).send("LOGIN ERR");
  }
});

module.exports = router;
